<?php
/**
 * Date: 2024-09-20
 */

namespace module\document\controller\admin;

use app\library\Controller;
use module\document\front\form\DocumentForm;
use module\document\logic\DocumentLogic;
use mof\annotation\AdminPerm;
use mof\annotation\Inject;
use mof\ApiResponse;
use think\response\Json;

#[AdminPerm(
    title: '文档管理', url: 'document/document', actions: '*',
    sort: 3, icon: 'Document', group: 'system:system:system'
)]
class Document extends Controller
{
    #[Inject]
    protected DocumentLogic $logic;

    #[Inject]
    protected DocumentForm $form;

    public function index(): Json
    {
        return ApiResponse::success(
            $this->logic->paginate($this->request->searcher())
        );
    }

    public function create(): Json
    {
        return ApiResponse::success($this->form->build());
    }

    public function edit($id): Json
    {
        return ApiResponse::success(
            $this->form->build($this->logic->read($id))
        );
    }

    public function read($id): Json
    {
        return ApiResponse::success($this->logic->read($id));
    }

    public function update($id): Json
    {
        $article = $this->logic->update(
            $id, $this->form->withScene('edit')->withFixed(['id' => $id])->get()
        );
        return ApiResponse::success($article);
    }

    public function save(): Json
    {
        $article = $this->logic->save($this->form->get());
        return ApiResponse::success($article);
    }

    public function delete($id): Json
    {
        $this->logic->delete($id);
        return ApiResponse::success();
    }

    public function updates($id, $field, $value): Json
    {
        $this->logic->updates($id, $field, $value);
        return ApiResponse::success();
    }

    public function deletes(): Json
    {
        $this->logic->deletes($this->request->getPostIds());
        return ApiResponse::success();
    }

}